﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class Enterprise
    {
        private static string currTableName = WebConfig.TablePrefix + "enterprise";

        public EnterpriseInfo GetEnterpriseInfo(DataRow dr)
        {
            EnterpriseInfo model = new EnterpriseInfo();
            model.id = (int)dr["id"];
            model.name = (string)dr["name"];
            model.logo = (string)dr["logo"];
            model.addtime = (DateTime)dr["addtime"];
            model.state = (int)dr["state"];
            model.url = (string)dr["url"];
            model.tip = (int)dr["tip"];
            model.tag = (int)dr["tag"];
            model.abundant = (int)dr["abundant"];
            model.sincerity = (int)dr["sincerity"];
            model.hits = (int)dr["hits"];
            model.orders = (int)dr["orders"];
            model.commend = (int)dr["commend"];
            model.stick = (int)dr["stick"];
            model.info = (string)dr["info"];
            model.description = (string)dr["description"];
            model.phone = (string)dr["phone"];
            model.address = (string)dr["address"];
            model.provice = (string)dr["provice"];
            model.city = (string)dr["city"];
            model.fax = (string)dr["fax"];
            model.code = (string)dr["code"];
            return model;
        }

        public EnterpriseInfo GetEnterpriseInfo(int id, string currPostfix)
        {
            EnterpriseInfo model = null;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from " + currTableName + currPostfix);
            strSql.Append(" where id=" + id.ToString());
            DataTable dt = DbHelper.TabQuery(strSql.ToString());
            if (dt.Rows.Count > 0)
            {
                model = GetEnterpriseInfo(dt.Rows[0]);
            }
            return model;
        }

        public bool Add(EnterpriseInfo model, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + currPostfix + "(");
            strSql.Append(" [name], [logo], [addtime], [state], [url], [tip], [tag], [abundant], [sincerity], [hits], [orders], [commend], [stick], [info], [description], [phone], [address], [provice], [city], [fax], [code]");
            strSql.Append(")");
            strSql.Append(" values (");

            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.name) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.logo) + "',");
            strSql.Append("Now(),");
            strSql.Append("'" + model.state.ToString() + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.url) + "',");
            strSql.Append("'" + model.tip.ToString() + "',");
            strSql.Append("'" + model.tag.ToString() + "',");
            strSql.Append("'" + model.abundant.ToString() + "',");
            strSql.Append("'" + model.sincerity.ToString() + "',");
            strSql.Append("'" + model.hits.ToString() + "',");
            strSql.Append("'" + model.orders.ToString() + "',");
            strSql.Append("'" + model.commend.ToString() + "',");
            strSql.Append("'" + model.stick.ToString() + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.info) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.description) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.phone) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.address) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.provice) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.city) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.fax) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.code) + "',");

            strSql.Append(")");

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool Update(EnterpriseInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName);
            strSql.Append(" set [name]='" + StringHelper.SqlFilter(model.name) + "',");
            strSql.Append("[info]='" + StringHelper.SqlFilter(model.info) + "',");
            strSql.Append("[description]='" + StringHelper.SqlFilter(model.description) + "',");
            strSql.Append(" where id=" + model.id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }



        public bool UpdateTip(EnterpriseInfo model, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + currPostfix + " set ");
            strSql.Append("tip=" + model.tip.ToString());
            strSql.Append(" where id=" + model.id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool UpdateLocked(int id, bool locked, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + currPostfix + " set ");
            strSql.Append("state=");
            strSql.Append(locked ? "1" : "0");
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool AddTip(int id, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + currPostfix + " set ");
            strSql.Append("tip=tip+1");
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }








        public bool Delete(int id, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName + currPostfix);
            strSql.Append(" where id=" + id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }











        public DataTable GetList(int topCount, string andWhere, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top " + topCount.ToString() + " * from  [" + currTableName + currPostfix + "] where 1=1 ");
            strSql.Append(andWhere);
            return DbHelper.TabQuery(strSql.ToString());
        }

        public DataTable GetList(int pageSize, int currPage, string andWhere, bool showSql, string currPostfix)
        {

            string sql = Sql.GetRePagingString(pageSize, currPage, "[" + currTableName + currPostfix + "]", "id", andWhere);
            if (showSql) //调试
            {
                System.Web.HttpContext.Current.Response.Write(sql);
            }
            return DbHelper.TabQuery(sql);
        }

        public string GetObjectString(string colname, string andWhere, string currPostfix)
        {

            Object obj = DbHelper.GetSingle(string.Format("select top 1 [{0}] from [" + currTableName + currPostfix + "] where 1=1 {1}", colname, andWhere));
            return obj == null ? "" : obj.ToString();
        }

        public DataTable GetIdList(string andWhere, string currPostfix)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [id] from [" + currTableName + currPostfix + "] where 1=1 ");
            strSql.Append(andWhere);
            return DbHelper.TabQuery(strSql.ToString());
        }

        public DataTable GetList(int pageSize, int currPage, string columns, string currPostfix, Object joininfo)
        {

            string sql = Sql.GetRePagingString(pageSize, currPage, currTableName + currPostfix, "id", columns, (JoinInfo)joininfo);
            return DbHelper.TabQuery(sql);
        }




        public int GetCount(string andWhere, string currPostfix)
        {
            string sql = "select count(1) from [" + currTableName + currPostfix + "] where 1=1 " + andWhere;
            return (int)DbHelper.GetSingle(sql);
        }

        public int GetTipCount(string andWhere, string currPostfix)
        {

            string sql = "select tip from [" + currTableName + currPostfix + "] where 1=1 " + andWhere;
            return (int)DbHelper.GetSingle(sql);
        }




        public string Help()
        {
            return @"mailto:549002835@qq.com";
        }


    }
}
